﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UscSunShine.Models;
using System.Data.Entity;
using System.Threading.Tasks;
using Webdiyer.WebControls.Mvc;

namespace UscSunShine.Mvc.Areas.SuperAdmin.Controllers
{
    [Description("申请信息审核")]
    public class RequestAuditController : UscSunShine.Mvc.Controllers.BaseController
    {
        // GET: SuperAdmin/RequestAudit
        /// <summary>
        /// 取待审核的数据
        /// </summary>
        /// <returns></returns>
        private IQueryable<Request> RequestsNeedAudit()
        {
            //取允许浏览的处理状态
            var status = UscSunShine.Models.Request.StatusNeedAudit;
            //取数据
            //未审核的不显示
            var requests = _db.Requests.Include(t => t.Department)
                                      .Where(t => status.Contains(t.ProcessStatus));
            return requests;
        }

        /// <summary>
        /// 待审回复列表
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        [Description("待审核信息列表")]
        public ActionResult Index(int index=1)
        {
            //取数据
            //只显示可以审核的
            var requests = RequestsNeedAudit();
            //根据类别与受理时间排序
            requests = requests.OrderByDescending(t => t.AcceptDate);

            return View(requests.ToPagedList(index, 10));
        }

        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="id">申请ID</param>
        /// <param name="isPassed">是否通过审核</param>
        /// <returns></returns>
        private async Task<ActionResult> Audit(Request request, bool isPassed)
        {
            if (request == null)
            {
                return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest);
            }
            //取出申请信息
            var model = await _db.Requests.FindAsync(request.ID);

            if (model == null)
            {
                return HttpNotFound();
            }

            //是否满足待审核条件
            if (UscSunShine.Models.Request.StatusNeedAudit.Contains(model.ProcessStatus))
            {
                if (isPassed)
                {
                    //更改处理部门、信息类别
                    model.DepartmentID = request.DepartmentID;
                    model.RequestCategory = request.RequestCategory;
                }
                //审核信息，修改申请信息的状态
                model.Audit(isPassed);
                await _db.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            else
            {
                return Content("不符合审核条件");
            }
        }

        /// <summary>
        /// 审核用详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Description("审核信息")]
        public async Task<ActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest);
            }
            Request request = await _db.Requests.Include(t => t.Department)
                                               .SingleAsync(t => t.ID == id);
            if (request == null)
            {
                return HttpNotFound();
            }
            ViewBag.DepartmentID = new SelectList(_db.Departments, "ID", "Name");
            return View(request);
        }

        /// <summary>
        /// 审核通过
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Description("审核信息，保存")]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Edit([Bind(Include = "ID,DepartmentID,RequestCategory,AcceptNumber,Title,Content,Username,ContactPhone,Email,ExpectResolveDate,RespondContent,ProcessDate")] Request request, string selectedRole)
        {
            if (ModelState.IsValid)
            {
                if (selectedRole == "Pass")
                {
                    return await Audit(request, true);
                }
                else
                {
                    return await Audit(request, false);
                }
            }

            ViewBag.DepartmentID = new SelectList(_db.Departments, "ID", "Name", request.DepartmentID);

            return View(request);
        }
 

        // GET: /Request/Delete/5
        [Description("删除信息")]
        public async Task<ActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest);
            }
            Request request = await _db.Requests.Include(t => t.Department)
                                               .SingleAsync(t => t.ID == id);
            if (request == null)
            {
                return HttpNotFound();
            }
            return View(request);
        }

        // POST: /Request/Delete/5
        [Description("删除信息，保存")]
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> DeleteConfirmed(int id)
        {
            Request request = await _db.Requests.FindAsync(id);
            _db.Requests.Remove(request);
            await _db.SaveChangesAsync();
            return RedirectToAction("Index");
        }
    }
}